import utils from './utils';
let vshow = {};

/*** history */
vshow.his = {};
vshow.his.hisKey = 'hisData';
vshow.his.hisCount = 30;
vshow.his.pushDataHis = function (vData) {
    var item = {
        'vid': vData.vid,
        'vpic': vData.vpic,
        'vname': vData.vname,
        'hdate': utils.formatTime(new Date()),
        'vnote': vData.vnote,
        'vpublishyear': vData.vpublishyear,
        'vpublisharea': vData.vpublisharea,
        'tnamel2': vData.tnamel2
    };
    var hisData = JSON.parse(localStorage.getItem(this.hisKey)) || [];
    if (hisData.length == this.hisCount) {
        hisData.pop();
    }
    for (const i in hisData) {
        if (hisData[i].vid == item.vid) {
            hisData.splice(i, 1);
            break;
        }
    }
    hisData.unshift(item);
    localStorage.setItem(this.hisKey, JSON.stringify(hisData));
}

vshow.his.getDataHis = function () {
    return JSON.parse(localStorage.getItem(this.hisKey)) || [];
}
vshow.his.cleanHis = function () {
    localStorage.removeItem(this.hisKey);
}

/* cache */
vshow.cache = {};
vshow.cache.cacheKey = "cacheData";
vshow.cache.cacheStatus = function(vid,num,state){
    //缓存后的状态处理
    var itemKey = "c_"+vid;
    var cacheData = this.getCacheData();
    if (cacheData.hasOwnProperty(itemKey) ) {
        //检查是否存在缓存
        let f = false;
        let playData = cacheData[itemKey].playData;
        for(const i in playData){
            var playDataItem = playData[i];
            if(playDataItem.num == num){
                //存在
                playDataItem.state = state;
                playData[i] = playDataItem;
                break;
            }
        }
        cacheItem.playData = playData;
    }
    this.setCacheData(setCacheData);
}
vshow.cache.pushCacheData = function (vData,item) {
    item.state = 0;//默认缓存状态
    var itemKey = "c_"+vData.vid;
    var cacheItem = {
        'vid': vData.vid,
        'vpic': vData.vpic,
        'vname': vData.vname,
        'hdate': utils.formatTime(new Date()),
        'vnote': vData.vnote,
        'vpublishyear': vData.vpublishyear,
        'vpublisharea': vData.vpublisharea,
        'tnamel2': vData.tnamel2,
        'playData':[item]
    };
    var cacheData = this.getCacheData();
    // for (const k in cacheData) {
        if (cacheData.hasOwnProperty(itemKey) ) {
            //检查是否存在缓存
            let f = false;
            let playData = cacheData[itemKey].playData;
            for(const i in playData){
                var playDataItem = playData[i];
                if(playDataItem.num == item.num){
                    //存在
                    f = true;
                    break;
                }
            }
            if(!f){
                playData.push(item);
                playData.sort(function(a,b){
                    return a.num - b.num;
                });
                cacheItem.playData = playData;
            }else{
                return -1;
            }
        }
    // }
    cacheData[itemKey] = cacheItem;
    this.setCacheData(setCacheData);
    return 1;
}

vshow.cache.getCacheData = function () {
    return JSON.parse(localStorage.getItem(this.cacheKey)) || {};
}
vshow.cache.setCacheData = function(cacheData){
    localStorage.setItem(this.cacheKey,JSON.stringify( cacheData));
}
vshow.cache.removeCacheData = function(vid){
    var cacheData = this.getCacheData();
    delete cacheData['c_'+vid];
    localStorage.setItem(this.collectKey,JSON.stringify(cacheData));
}
vshow.cache.clearCacheData=function(){
    localStorage.setItem(this.collectKey,JSON.stringify({}));
}

/***collect */
vshow.collect = {};
vshow.collect.collectKey = "collectData";
vshow.collect.pushCollectData=function(vData){
    var itemKey = "c_"+vData.vid;
    var item = {
        'vid': vData.vid,
        'vpic': vData.vpic,
        'vname': vData.vname,
        'hdate': utils.formatTime(new Date()),
        'vnote': vData.vnote,
        'vpublishyear': vData.vpublishyear,
        'vpublisharea': vData.vpublisharea,
        'tnamel2': vData.tnamel2
    };
    var collectData = this.getCollectData();
    if(collectData.hasOwnProperty(itemKey)){
        return;
    }
    collectData[itemKey] = item;
    localStorage.setItem(this.collectKey,JSON.stringify(collectData));
}
vshow.collect.removeCollectData=function(vid){
    var collectData = this.getCollectData();
    delete collectData['c_'+vid];
    localStorage.setItem(this.collectKey,JSON.stringify(collectData));
}
vshow.collect.getCollectData = function () {
    return JSON.parse(localStorage.getItem(this.collectKey)) || {};
}
vshow.collect.hasCollectDate = function(vid){
    return this.getCollectData().hasOwnProperty("c_"+vid);
}
vshow.collect.clearCollectData=function(vid){
    localStorage.setItem(this.collectKey,JSON.stringify({}));
}

window.vshow = vshow;